package com.iostream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 20/8/2021 下午 3:24
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Test the use of java.io.FileInputStream and java.io.FileOutputStream
 */
public class TestFileStream {

    public static void main(String[] args) {
        FileInputStream fis1 = null;
        FileOutputStream fos1 = null;

        try {
            //获取目标位置
            File srcFile = new File("day11/txt/hello.txt");
            File destFile = new File("day11/txt/hello3.txt");

            //获取流
            fis1 = new FileInputStream(srcFile);
            fos1 = new FileOutputStream(destFile);

            //接收数据
            byte[] cbuf = new byte[1024];

            //接收数据的大小
            int len = 0;

            //数据的存取
            while((len = fis1.read(cbuf)) != -1) {

                fos1.write(cbuf,0,len);

            }
            System.out.println("Data transfer completed!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(fos1 != null) {

                try {
                    fos1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(fis1 != null) {

                try {
                    fis1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }

}
